layui.define(['table', 'form','my_send'], function(exports){
    var $ = layui.$
    ,table = layui.table
    ,form = layui.form
        ,http_proxy = layui.http_proxy;
    //用户管理
    table.render({
      elem: '#LAY-user'
      ,url: http_proxy('/user/getUserByParams') //模拟接口
      ,parseData:function(res){
        return{
          "code":0
          ,"count":res.total
          ,"data":res.rows
          ,'msg':''
        }
      }
      ,cols: [[
         {field: 'uid', title: 'ID'}
        ,{field: 'userName', title: '用户名'}
        ,{field: 'password', title: '密码'}
        ,{field: 'stationName', title: '站名'}
        ,{field: 'userRole', title: '角色',templet:function(d){
          return d.userRole.roleName
        }}
        ,{title: '操作', minWidth: 150, align: 'center', fixed: 'right', toolbar: '#LAY-user-tool'}
      ]]
      ,page: true
      ,limit: 10
      ,limits: [10, 15, 20, 25, 30]
      ,text: '对不起，加载出现异常！'
    });
    
    //监听工具条
    table.on('tool(LAY-user)', function(obj){
      var data = obj.data;
      if(obj.event === 'del'){
        layer.confirm('确定删除此用户？', function(index){
          obj.del();
          layer.close(index);
        });
      } else if(obj.event === 'edit'){
        layer.open({
          type: 2
          ,title: '编辑'
          ,content: 'userForm.html?uid='+ data.uid
          ,maxmin: true
          ,area: ['550px', '350px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
            ,submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
  
            //监听提交
            iframeWindow.layui.form.on('submit(layuiadmin-app-form-edit)', function(data){
              var field = data.field; //获取提交的字段
              
              //提交 Ajax 成功后，静态更新表格中的数据
              //$.ajax({});              
              obj.update({
                label: field.label
                ,title: field.title
                ,author: field.author
                ,status: field.status
              }); //数据更新
              
              form.render();
              layer.close(index); //关闭弹层
            });  
            
            submit.trigger('click');
          }
        });
      }
    });
  
    //权限管理
    table.render({
      elem: '#LAY-privilege'
      ,url: http_proxy('/userPrivilege/getUserPrivilegeByParams') //模拟接口
      ,parseData:function(res){
        return{
          "code":0
          ,"count":res.total
          ,"data":res.rows
          ,'msg':''
        }
      }
      ,cols: [[
        {field: 'upid', width: 100, title: 'ID'}
        ,{field: 'privilege', title: '权限位', minWidth: 100}
        ,{field: 'privilegeName', title: '权限名', minWidth: 100}
        ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#LAY-privilege-tool'}
      ]]
      ,page: true
      ,limit: 10
      ,limits: [10, 15, 20, 25, 30]
      ,text: '对不起，加载出现异常！'
    });
    
    //监听工具条
    table.on('tool(LAY-privilege)', function(obj){
      var data = obj.data;
      if(obj.event === 'del'){
        layer.confirm('确定删除此分类？', function(index){
          obj.del();
          layer.close(index);
        });
      } else if(obj.event === 'edit'){
        var tr = $(obj.tr);
        layer.open({
          type: 2
          ,title: '编辑分类'
          ,content: 'privilegeForm.html?upid='+ data.upid
          ,area: ['550px', '300px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            //获取iframe元素的值
            var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-tags")
            ,tags = othis.find('input[name="tags"]').val();
            
            if(!tags.replace(/\s/g, '')) return;
            
            obj.update({
              tags: tags
            });
            layer.close(index);
          }
          ,success: function(layero, index){
            //给iframe元素赋值
            var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-tags").click();
            othis.find('input[name="tags"]').val(data.tags);
          }
        });
      }
    });
  
    //用户权限管理
    table.render({
      elem: '#LAY-userPrivilege'
      ,url:http_proxy('/userRole/getUserRoleByParams') //模拟接口
      ,parseData:function(res){
        return{
          "code":0
          ,"count":res.total
          ,"data":res.rows
          ,'msg':''
        }
      }
      ,cols: [[
        {field: 'urid', width: 100, title: 'ID'}
        ,{field: 'roleName', title: '角色名', minWidth: 100}
        ,{field: 'privileges', title: '权限名',height:300,templet:function(d){
          var privilegesName="";
          if(d.privileges.length>0){
            for(var i=0;i<d.privileges.length;i++){
              privilegesName+=d.privileges[i].privilegeName+"</br>"
            }
          }
          return privilegesName
         // debugger
        
}}
        ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#LAY-userPrivilege-tool'}
      ]]
      ,page: true
      ,limit: 10
      ,limits: [10, 15, 20, 25, 30]
      ,text: '对不起，加载出现异常！'
    });
    
    //监听工具条
    table.on('tool(LAY-userPrivilege)', function(obj){
      var data = obj.data;
      if(obj.event === 'del'){
        layer.confirm('确定删除此条评论？', function(index){
          obj.del();
          layer.close(index);
        });
      } else if(obj.event === 'edit') {
        layer.open({
          type: 2
          ,title: '编辑'
          ,content: 'userPrivilegeForm.html'
          ,area: ['550px', '300px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
            ,submitID = 'layuiadmin-app-comm-submit'
            ,submit = layero.find('iframe').contents().find('#'+ submitID);
  
            //监听提交
            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
              var field = data.field; //获取提交的字段
              
              //提交 Ajax 成功后，静态更新表格中的数据
              //$.ajax({});
              table.reload('LAY-app-content-comm'); //数据刷新
              layer.close(index); //关闭弹层
            });  
            
            submit.trigger('click');
          }
          ,success: function(layero, index){
            
          }
        });
      }
    });
  
    exports('system', {})
  });